//
//  PMineResumeTradeViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/25.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineResumeTradeViewController.h"
#import "LYSelectCompanyCell.h"
#import "LYSelectCompanyHeader.h"

@interface PMineResumeTradeViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) LYSelectCompanyHeader *header;
@property (nonatomic, strong) NSMutableArray *selectArray;
@end

@implementation PMineResumeTradeViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        
        for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.tradeArray.count; i++) {
            PublicCategoryElementModel *model = [[DataToolsControl sharedManager].allCategoryInfoModel.tradeArray[i] copy];
            model.isSelect = NO;
            model.labelid = i;
            model.itemWidth = [model.name sizeOfFont:Font(Font_14) maxSize:CGSizeMake(MAXFLOAT, 30*Scale)].width+20*Scale;
            [self.dataArray addObject:model];
        }
    }
    return _dataArray;
}

- (NSMutableArray *)selectArray {
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"期望行业";
    
    [self initUI];
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.header = [[LYSelectCompanyHeader alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 90)];
    self.header.selectArray = self.selectArray;
    __weak typeof(self) weakSelf = self;
    self.header.block = ^(NSInteger index){
        PublicCategoryElementModel *model = weakSelf.selectArray[index];
        model.isSelect = NO;
        [weakSelf.selectArray removeObjectAtIndex:index];
        [weakSelf reloadheader];
    };
    [self.view addSubview:self.header];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, EndY(self.header), WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-ViewHeight(self.header)) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableview];
    
    [self.tableview registerClass:[LYSelectCompanyCell class] forCellReuseIdentifier:kCellIdentifier_LYSelectCompanyCell];
}
- (void)okBtnTap {
    if (self.selectArray.count) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(PMineResumeTradeViewControllerBackInfo:)]) {
            [self.delegate PMineResumeTradeViewControllerBackInfo:self.selectArray];
        }
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        SVPHUDError(@"您还未选择");
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYSelectCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYSelectCompanyCell forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PublicCategoryElementModel *model = self.dataArray[indexPath.row];
    if (self.selectArray.count <= 2 && model.isSelect == NO) {
        model.isSelect = YES;
        [self.selectArray addObject:model];
        [self reloadheader];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}
#pragma mark -- Action
- (void)reloadheader{
    self.header.selectArray = self.selectArray;
    self.tableview.top = self.header.bottom;
    self.tableview.frame = CGRectMake(0, EndY(self.header), WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-ViewHeight(self.header));
    [self.tableview reloadData];
}

@end
